package com.agilya.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.agilya.modele.Proposition;
import com.agilya.modele.QCM;
import com.agilya.modele.Question;

/**
 * Servlet implementation class AfficherQuestion
 * 
 * Affiche le résultat d'un QCM
 */
@WebServlet("/AfficherResultat")
public class AfficherResultat extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AfficherResultat() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// NOTHING

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession();

		// On récupère la liste des questions session
		QCM qcm = (QCM) session.getAttribute("qcm");

		// On récupère la liste des réponses de la session
		HashMap<Question, List<Proposition>> reponsesQCM;
		reponsesQCM = (HashMap<Question, List<Proposition>>) session.getAttribute("reponsesQCM");

		double noteApprenantFinale = qcm.calculerNoteApprenant(reponsesQCM);
		request.setAttribute("noteApprenantFinale", noteApprenantFinale);

		request.setAttribute("page", "/afficherResultat.jsp");
		RequestDispatcher rd = request.getRequestDispatcher("/page.jsp");
		rd.forward(request, response);
	}

}
